package com.mingshashan.building.easy.core.controller;

import com.mingshashan.building.easy.core.service.IDictionarySerivce;
import com.mingshashan.building.easy.core.vo.DictionaryCreationVO;
import com.mingshashan.building.easy.core.vo.DictionaryTree;
import com.mingshashan.building.easy.core.vo.DictionaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * DictionaryController
 *
 * @author jasonxu
 */
@Api("字典管理")
@RestController
@RequestMapping("/dicts")
public class DictionaryController {

    @Autowired
    private IDictionarySerivce dictionarySerivce;

    @ApiOperation("新增字典数据，如果是子项，则需要填parentCode")
    @PostMapping
    public DictionaryVO add(@RequestBody DictionaryCreationVO dictionaryCreationVO) {
        return dictionarySerivce.add(dictionaryCreationVO);
    }

    @ApiOperation("根据Id获取字典信息")
    @GetMapping("{id}")
    public DictionaryVO getById(@PathVariable("id") Long id) {

        return dictionarySerivce.getById(id);
    }

    @ApiOperation("根据code获取字典信息")
    @GetMapping()
    public DictionaryVO getByCode(@RequestParam("code") String code) {
        return dictionarySerivce.getByCode(code);
    }

    @ApiOperation("根据id，更新字典信息")
    @PutMapping("{id}")
    public DictionaryVO update(@PathVariable("id") Long id, DictionaryVO dictionaryVO) {
        return dictionarySerivce.update(id, dictionaryVO);
    }

    @DeleteMapping("{id}")
    public void delete(@PathVariable("id") Long id) {
        dictionarySerivce.delete(id);
    }

    @ApiOperation("查询树结构信息，如果没有rootCode，则全部查询，如果有则从rootCode查询")
    @GetMapping("/tree")
    public DictionaryTree getRootDictionaryTree(@RequestParam(name = "rootCode", required = false) String rootCode) {
        return dictionarySerivce.getRootDictionaryTree(rootCode);
    }


}
